package top.fangw.common.generator.util;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import org.apache.ibatis.annotations.Mapper;
import top.fangw.common.core.entity.BaseEntity;

import java.util.Collections;

/**
 * @description: 代码生成
 * @author: fgw
 * @createDate: 2024/1/12
 */
public class GeneratorUtil {
    public static void generator(String ip, String port, String username, String password, String databaseName, String modulePath, String packageName, String tables) {
        String projectPath = System.getProperty("user.dir");
        String url = "jdbc:mysql://" + ip + ":" + port + "/" + databaseName + "?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
        FastAutoGenerator.create(url, username, password)
                .globalConfig((scanner, builder) -> {
                    builder
                            // 禁止打开输出目录
                            .disableOpenDir()
                            // 指定输出目录
                            .outputDir(projectPath + "/" + modulePath + "/src/main/java")
                            // 开启 swagger 模式
                            .enableSpringdoc()
                            // 作者名
                            .author("fangw")
                            // 注释日期
                            .commentDate("yyyy-MM-dd");
                })
                .packageConfig((scanner, builder) -> {
                    builder
                            // Entity 包名
                            .entity("pojo.entity")
                            // 路径配置信息
                            .pathInfo(Collections.singletonMap(OutputFile.xml, modulePath + "/src/main/resources/mapper/"))
                            // 父包名
                            .parent(packageName);

                })
                .strategyConfig((scanner, builder) -> {
                    builder
                            //增加表匹配(内存过滤)
                            .addInclude(tables)
                            // 增加过滤表前缀
                            .addTablePrefix("t_","tbl_","g_tbl_", "g_t_")
                            .entityBuilder()
                            // 开启 lombok 模式
                            .enableLombok()
                            // 开启链式模式
                            .enableChainModel()
                            // 开启生成实体时生成字段注解
                            .enableRemoveIsPrefix()
                            // 开启生成实体时生成字段注解
                            .enableTableFieldAnnotation()
                            .superClass(BaseEntity.class)
                            // 添加忽略字段
                            .addIgnoreColumns("id","created_by","created_by_name","created_at","updated_by","updated_by_name","updated_at","del_flag","remark")
//                            // 开启生成实体时生成字段注解
//                            .logicDeleteColumnName("del_flag")
                            .controllerBuilder()
                            // 开启生成 @RestController 控制器
                            .enableRestStyle()
                            // 开启驼峰转连字符
                            .enableHyphenStyle()
                            .mapperBuilder()
                            // 开启 @Mapper 注解
                            .mapperAnnotation(Mapper.class)
                            // 开启 BaseResultMap
                            .enableBaseResultMap()
                            // 开启 baseColumnList
                            .enableBaseColumnList();
                })
                .execute();
    }
}
